import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

class myJFrame2 extends JFrame{
    JPanel p;
    JLabel l1,l2;
    JTextField t1,t2;
    JButton b1,b2;

    public myJFrame2(){
        super("用户注册登录");
        p=new JPanel(null);

        l1=new JLabel("用户名:");
        l2=new JLabel("密  码:");
        t1=new JTextField(20);
        t2=new JTextField(20);
        b1=new JButton("注册");
        b2=new JButton("登录");

        l1.setFont(new Font("楷体",Font.BOLD,40));
        l2.setFont(new Font("楷体",Font.BOLD,40));
        t1.setFont(new Font("楷体",Font.BOLD,40));
        t2.setFont(new Font("楷体",Font.BOLD,40));
        b1.setFont(new Font("楷体",Font.BOLD,40));
        b2.setFont(new Font("楷体",Font.BOLD,40));


        l1.setBounds(120, 70, 150, 50);
        t1.setBounds(280, 70, 240, 50);
        l2.setBounds(120, 150, 150, 50);
        t2.setBounds(280, 150, 240, 50);
        b1.setBounds(190, 230, 130, 50);
        b2.setBounds(330, 230, 130, 50);

        p.add(l1);
        p.add(l2);
        p.add(t1);
        p.add(t2);
        p.add(b1);
        p.add(b2);
        this.add(p);

        setBounds(200,300,685,450);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/yonghu?useSSL=false";
                String user = "root";
                String password = "123456";
                Connection conn = null;
                Statement stmt = null;
                ResultSet rSet = null;
                try {
                    Class.forName(driver);
                    conn = DriverManager.getConnection(url, user, password);
                    stmt = conn.createStatement();
                    String name = t1.getText();
                    String pwd = t2.getText();

                    rSet = stmt.executeQuery("select id,name,pwd from User where name='" + name + "'");
                    if (rSet.next()) {
                        JOptionPane.showMessageDialog(p, "该用户已存在");
                    } else {
                        String insertsql = "insert into User(name,pwd) values('" + name + "','" + pwd + "')";
                        stmt.executeUpdate(insertsql);
                        JOptionPane.showMessageDialog(p, "注册成功，用户信息已保存");
                        t1.setText("");
                        t2.setText("");
                    }

                    stmt.close();
                    conn.close();
                }catch(ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch(SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/User";
                String user = "root";
                String password = "123456";
                Connection conn = null;
                Statement stmt = null;
                ResultSet rSet = null;
            }
        });

    }
}
public class demo2 {
    public static void main(String[] args) {
        new myJFrame2();
    }
}
